<?php

namespace app\index\controller;

use app\common\controller\Front;
use app\common\logic\CacheKey;
use app\common\serve\Buy;
use app\common\serve\User;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;
use think\exception\PDOException;
use think\Validate;

/**
 * 产品 - 首页
 * Class Invest
 * @package app\index\controller
 */
class Invest extends Front
{
    /**
     * 列表页
     * @throws DbException
     */
    public function index()
    {
        $list = model('Borrow')->field('*')->where([])->select();
        $this->assign('borrows', $list);
        return $this->fetch();
    }

    /**
     * 详细页
     * @throws DbException
     */
    public function detail($id)
    {
        if ($id < 1) $this->error('参数缺失！');

        $borrow = model('Borrow')->find($id);
        $this->assign('borrow', $borrow);

        $invested = model('BorrowInvest')->where(['borrow_id' => $id])->order('id DESC')->paginate(20, true);
        $this->assign('invest', $invested);
        $this->assign('page', $invested->render());

        $account_money = 0.00;
        if ($this->uid > 0) {
            $account_money = Db::name('user_money')->where(['uid' => $this->uid])->value('account_money');
        }
        $this->assign('account_money', $account_money);

        return $this->fetch();
    }

    /**
     * 购买
     * @throws DataNotFoundException
     * @throws DbException
     * @throws Exception
     * @throws ModelNotFoundException
     * @throws PDOException
     */
    public function buy()
    {
        if ($this->request->isPost()) {

            $params = $this->request->param();

            //验证TOKEN
            if (!Validate::token('__token__', '', ['__token__' => $params['__token__']])) {
                $this->error('请勿重复购买,请刷新页面');
            }

            $validate = new Validate([
                'borrow_id' => 'require',
//                'pay_pass' => 'require|max:6|number',
                'money' => 'require|number',
            ], [
                'borrow_id' => '项目ID必传',
                'money' => '购买金额必传',
//                'pay_pass' => '支付密码必传',
                'money.number' => '金额必须是数字',
//                'pay_pass.number' => '支付密码必须是数字',
            ]);

            if (!$validate->check($params)) {
                $this->error('参数出错请检查', '', $validate->getError());
            }

//            if (!User::checkPayPass($this->uid, $params['pay_pass'])) {
//                $this->error('支付密码不正确，请重新输入');
//
//                $count = cache(CacheKey::PAY_PASS_ERROR_COUNT . $this->uid) ?: 0;
//                if ($count == 0) {
//                    $this->error('支付密码不正确，请重新输入');
//                }
//
//                if ($count > 0 && $count <= 3) {
//                    $count++;
//                    cache(CacheKey::PAY_PASS_ERROR_COUNT . $this->uid, $count, 900);
//                    $this->error('支付密码已经错误' . $count . '次,请谨慎操作');
//                } else {
//                    $this->error('支付密码错误次数达到上限，请15分钟后再操作');
//                }
//            }

            $result = Buy::prePay([
                'uid' => $this->uid,
                'borrow_id' => $params['borrow_id'],
                'money' => $params['money'],
                'ip' => $this->request->ip(),
                'way' => $this->isMobile() ? 2 : 1
            ]);

            if (is_array($result) && $result['code'] == 1) {
                $this->success($result['msg']);
            } else {
                $this->success($result['msg']);
            }
        }
        $this->redirect('/');
    }
}
